package com.yanqu.road.server.cmd.activity.goodsLimit;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.goodsLimit.GoodsLimitModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

/**
 * 获取道具使用数量,需要传入道具id和活动id
 */
@Cmd(code = GameProtocol.S_ACTIVITY_GET_GOODS_LIMIT_DETAIL, desc = "获取活动限制道具使用数量")
public class GoodsLimitDetailCmd extends AbstractCommand {

    private int goodsId;

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityProto.ActivityGetGoodsLimitDetailReqMsg reqMsg = ActivityProto.ActivityGetGoodsLimitDetailReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        ActivityProto.ActivityGetGoodsLimitDetailRespMsg.Builder builder = ActivityProto.ActivityGetGoodsLimitDetailRespMsg.newBuilder();
        builder.setRet(0);
        builder.setActivityId(activityId);
        builder.setGoodsId(goodsId);
        long count = player.getModule(GoodsLimitModule.class).getGoodsUseCount(goodsId, activityId);
        builder.setNum(count);
        player.sendPacket(Protocol.U_ACTIVITY_GET_GOODS_LIMIT_DETAIL, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ActivityProto.ActivityGetGoodsLimitDetailRespMsg.Builder builder = ActivityProto.ActivityGetGoodsLimitDetailRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(Protocol.U_ACTIVITY_GET_GOODS_LIMIT_DETAIL, builder);
        }
    }
}
